package br.com.jogo.neg;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class BattleField {

	public BattleGrid battleGrid = new BattleGrid();
	// public final List<Unit> units = new ArrayList<Unit>();

	public final Team teamA = new Team(Team.LEFT);
	public final Team teamB = new Team(Team.RIGHT);

	public void update(float delta) {

		battleGrid.clearTiles();

		for (int i = 0; i < teamA.units.size(); i++) {
			battleGrid.addElement(teamA.units.get(i));
		}

		for (int i = 0; i < teamB.units.size(); i++) {
			battleGrid.addElement(teamB.units.get(i));
		}

	}

	public void drawBackground(SpriteBatch spritebatch, float delta) {
		battleGrid.Draw(spritebatch, delta);
	}

	public void drawTeams(SpriteBatch spritebatch, float delta) {

		drawUnits(spritebatch, delta, teamA);
		drawUnits(spritebatch, delta, teamB);
	}

	private void drawUnits(SpriteBatch spritebatch, float delta, Team team) {
		for (int i = 0; i < team.units.size(); i++) {
			team.units.get(i).Draw(spritebatch, delta);
		}
	}

	public Tile getTileAtPoint(float x, float y) {

		if (y < 0 || y > 150) {
			return null;
		}

		if (x < 0 || x > 480) {
			return null;
		}

		int coordX, coordY;

		coordX = (int) (x / Tile.TILE_WIDTH);
		coordY = (int) (y / Tile.TILE_HEIGHT);

		return battleGrid.tiles[coordX][coordY];
	}

	public Tile getTileAtPoint(Vector3 localPoint) {
		return this.getTileAtPoint((int) localPoint.x,
				(int) localPoint.y);

	}

}
